//
//  LPYearAndMonthPickView.m
//  LovingPartyBuilding
//
//  Created by 刘应平 on 17/5/15.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "LPYearAndMonthPickView.h"

@implementation LPYearAndMonthPickView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor zj_colorWithR:20 G:20 B:20 Al:0.3];
        
        self.backView = [UIView new];
        self.backView.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.backView];
        self.backView.sd_layout.leftEqualToView(self).bottomEqualToView(self).widthIs([UIScreen zj_screenWith]).heightIs(250 * [UIScreen zj_screenHScale]);
        
        UIView *sedong = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, 50 * [UIScreen zj_screenHScale])];
        sedong.backgroundColor = [UIColor themeGrayColor];
        [self.backView addSubview:sedong];
        
        UIButton *cancle =[[UIButton alloc] initWithFrame:CGRectMake(30 * [UIScreen zj_screenWScale], 10 * [UIScreen zj_screenHScale], 50 * [UIScreen zj_screenWScale], 30 * [UIScreen zj_screenHScale])];
        cancle.tag = 1;
        [cancle setTitle:@"取消" forState:UIControlStateNormal];
        [cancle addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
        [cancle setTitleColor: [UIColor colorWithRed:0.26f green:0.56f blue:1.00f alpha:1.00f] forState:UIControlStateNormal];
        [self.backView addSubview:cancle];
        
        
        UIButton *sure = [[UIButton alloc] initWithFrame:CGRectMake(self.width - 80 * [UIScreen zj_screenWScale], 10 * [UIScreen zj_screenHScale], 50 * [UIScreen zj_screenWScale], 30 * [UIScreen zj_screenHScale])];
        sure.tag = 2;
        [sure setTitle:@"确定" forState:UIControlStateNormal];
        [sure addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
        [sure setTitleColor: [UIColor colorWithRed:0.26f green:0.56f blue:1.00f alpha:1.00f] forState:UIControlStateNormal];
        [self.backView addSubview:sure];
        
        
        
        DVYearMonthDatePicker *pick = [[DVYearMonthDatePicker alloc] initWithFrame:CGRectMake(0, 50 * [UIScreen zj_screenHScale], self.width, 200 * [UIScreen zj_screenHScale])];
        
        
        NSDateFormatter *format = [[NSDateFormatter alloc]init];
        [format setDateFormat:@"YYYY"];
        NSInteger year = [[format stringFromDate:[NSDate date]] integerValue];
        [pick setupMinYear:year - 60 maxYear:year];
        [pick selectToday];
        pick.dvDelegate = self;
        [self.backView addSubview:pick];
        
       
        
        UIView *under = [[UIView alloc] initWithFrame:CGRectMake(0, 49 * [UIScreen zj_screenHScale], self.width, 1.5)];
        under.backgroundColor = [UIColor themeGrayColor];
        [self.backView addSubview:under];
       
       

    }
    return self;
}


#pragma mark - DVYearMonthDatePickerDelegate
- (void)yearMonthDatePicker:(DVYearMonthDatePicker *)yearMonthDatePicker didSelectedDate:(NSDate *)date{
    NSDateFormatter *format = [[NSDateFormatter alloc]init];
    [format setDateFormat:@"YYYY-MM"];
    self.dateString = [format stringFromDate:date];
    
}

- (void)clickButton:(UIButton *)sender{
    if (sender.tag == 1) {
        [self removeFromSuperview];
    }else{
        if (self.dateString.length == 0) {
            NSDateFormatter *format = [[NSDateFormatter alloc]init];
            [format setDateFormat:@"YYYY-MM"];
            self.dateString = [format stringFromDate:[NSDate date]];
        }
        if (self.delegate) {
            [self.delegate lpYearAndMonthWithView:self dateString:self.dateString index:self.row];
        }
        [self removeFromSuperview];
    }
}

- (void)show {
    [[UIApplication sharedApplication].keyWindow addSubview:self];
}
@end
